package com.start.form.engine.core;

import com.start.common.entity.StartDicData;
import com.start.common.exceptions.StartBusException;
import com.start.form.engine.core.entity.StartFormConfig;
import com.start.form.engine.core.model.StartFormConfigModel;
import com.start.form.engine.core.service.StartFormConfigService;
import com.start.form.engine.core.storage.StartBaseFormStorageEngineService;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class StartFormEngineImpl implements StartFormEngine {

    private final ConcurrentHashMap<String, StartBaseFormStorageEngineService> storageEngineMap;
    private final StartFormConfigService startFormConfigService;

    public StartFormEngineImpl(Map<String, StartBaseFormStorageEngineService> storageEngineMap,
                               StartFormConfigService startFormConfigService) {
        this.startFormConfigService = startFormConfigService;
        ConcurrentHashMap<String, StartBaseFormStorageEngineService> temp = new ConcurrentHashMap<>();
        Set<Map.Entry<String, StartBaseFormStorageEngineService>> entries = storageEngineMap.entrySet();
        for (Map.Entry<String, StartBaseFormStorageEngineService> entry : entries) {
            StartBaseFormStorageEngineService storageEngine=entry.getValue();
            String key = StringUtils.isAllBlank(storageEngine.getStorageKey()) ? entry.getKey() : storageEngine.getStorageKey();
            if(temp.containsKey(key)){
                throw new StartBusException("已存在相同 storageKey:"+key);
            }
            temp.put(key,storageEngine);
        }
        this.storageEngineMap=temp;
    }

    private void init() {

    }

    /**
     * 创建表单
     *
     * @param config
     */
    public void createForm(StartFormConfig config, boolean synchronization) {
        TableEngineService tableEngineService = getTableEngineService(config);
        tableEngineService.createTable(config);
        if (synchronization) {
            tableEngineService.synchronizationTable(config);
        }
    }

    /**
     * 修改表
     *
     * @param config          表配置
     * @param synchronization 是否同步
     */
    public void updateForm(StartFormConfig config, boolean synchronization) {
        TableEngineService tableEngineService = getTableEngineService(config);
        tableEngineService.updateTable(config);
        if (synchronization) {
            tableEngineService.synchronizationTable(config);
        }
    }


    /**
     * 根据表单编码获取表单配置
     *
     * @param formCode   表单编码
     * @param loadColumn 是否加载列信息
     * @return
     */
    @Override
    public StartFormConfigModel getFormByTableCode(String formCode, boolean loadColumn) {
        StartFormConfig formConfig = startFormConfigService.getOneByFormCode(formCode);
        if (formConfig == null) return null;
        return getForm(formConfig, loadColumn);
    }

    /**
     * 获取表单
     *
     * @param tableId
     * @param loadColumn
     * @return
     */
    @Override
    public StartFormConfigModel getForm(String tableId, boolean loadColumn) {
        StartFormConfig formConfig = startFormConfigService.getById(tableId);
        if (formConfig == null) return null;
        return getForm(formConfig, loadColumn);
    }

    /**
     * 获取表单
     *
     * @param formConfig
     * @param loadColumn
     * @return
     */
    @Override
    public StartFormConfigModel getForm(StartFormConfig formConfig, boolean loadColumn) {
        TableEngineService tableEngineService = getTableEngineService(formConfig);
        return tableEngineService.getTable(formConfig, loadColumn);
    }

    /**
     * 删除表单
     *
     * @param tableId
     */
    @Override
    public void deleteForm(String tableId) {
        StartFormConfig formConfig = startFormConfigService.getById(tableId);
        if (formConfig == null) {
            return;
        }
        getTableEngineService(formConfig).deleteTable(formConfig);
    }

    /**
     * 获取数据表服务
     *
     * @param tableId
     * @return
     */
    @Override
    public TableEngineService getTableEngineService(String tableId) {
        StartFormConfigModel form = getForm(tableId);
        return getTableEngineService(form);
    }

    @Override
    public TableEngineService getTableEngineService(StartFormConfig config) {
        TableEngineService tableEngineService = storageEngineMap.getOrDefault(config.getDataStorageType(), null);
        if (tableEngineService == null) {
            throw new StartBusException("未找到类型为：" + config.getDataStorageType() + "的数据表处理器");
        }
        return tableEngineService;
    }


    /**
     * 获取数据存储服务
     *
     * @param config
     * @return
     */

    @Override
    public TableDataEngineService getTableDataEngineService(StartFormConfig config) {
        TableDataEngineService tableDataEngineService = storageEngineMap.getOrDefault(config.getDataStorageType(), null);
        if (tableDataEngineService == null) {
            throw new StartBusException("未找到类型为：" + config.getDataStorageType() + "的数据表处理器");
        }
        return tableDataEngineService;
    }

    /**
     * 获取全部存储器
     *
     * @return
     */
    @Override
    public List<StartDicData> getAllStorage() {
        List<StartDicData> storages = new ArrayList<>();
        Collection<StartBaseFormStorageEngineService> values = storageEngineMap.values();
        for (TableEngineService service : values) {
            storages.add(new StartDicData(service.getStorageLabel(), service.getStorageKey()));
        }
        return storages;
    }
}
